package com.quiz;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;

public class Road {
	static int DEFAULT_WIDTH = 40;
	static int DEFAULT_HEIGHT = 40;
	static int IMPROVE_STEP = 1;
	static int DEGRADE_STEP = 2;

	int[][] matrix = {};

	int centerX = 6;
	int centerY = 4;

	int[][] road = { { 2, 7 }, { 3, 7 }, { 4, 7 }, { 5, 7 }, { 6, 7 },
			{ 6, 6 }, { 6, 5 }, { 6, 4 }, { 7, 4 }, { 8, 4 }, { 9, 4 },
			{ 10, 4 }, { 10, 3 }, { 10, 2 } };

	int currentPos = 7;

	boolean isHeaven = false;
	boolean isHell = false;

	Bitmap soul;
	Bitmap road_img;
	Context context;

	public Road(Context context) {
		this.context = context;
		soul = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.soul);
		road_img = BitmapFactory.decodeResource(context.getResources(),
				R.drawable.road);
	}

	/**
	 * tien len thien dang
	 */
	public void improve() {
		currentPos += IMPROVE_STEP;
		if (currentPos >= road.length) {
			isHeaven = true;
		}
	}

	/**
	 * lui ve phia dia nguc
	 */
	public void degrade() {
		currentPos -= DEGRADE_STEP;
		if (currentPos < 0) {
			isHell = true;
		}
	}

	/**
	 * kiem tra xem da den thien duong chua
	 */

	public boolean isInHeaven() {
		return isHeaven;
	}

	/**
	 * kiem tra xem co bi roi xuong dia nguc ko
	 */

	public boolean isInHell() {
		return isHell;
	}

	/**
	 * 
	 * @param canvas
	 */
	public void draw(Canvas canvas) {
		// int w = canvas.getWidth();
		// int h = canvas.getHeight();

		// // mang
		// for (int i = 0; i < w; i += DEFAULT_WIDTH) {
		// Paint paint = new Paint();
		// canvas.drawLine(i, 0, i, h, paint);
		// }
		// for (int j = 0; j < h; j += DEFAULT_HEIGHT) {
		// Paint paint = new Paint();
		// canvas.drawLine(0, j, w, j, paint);
		// }

		// center
		Paint paint = new Paint();
		paint.setColor(Color.MAGENTA);
		Rect rect = new Rect(centerX * DEFAULT_WIDTH, centerY * DEFAULT_HEIGHT,
				7 * DEFAULT_WIDTH, 5 * DEFAULT_HEIGHT);
		canvas.drawRect(rect, paint);

		// heaven
		paint = new Paint();
		paint.setColor(Color.MAGENTA);
		rect = new Rect(10 * DEFAULT_WIDTH, 0 * DEFAULT_HEIGHT,
				DEFAULT_WIDTH * 12, DEFAULT_HEIGHT * 2);
		canvas.drawRect(rect, paint);

		// hell
		paint = new Paint();
		paint.setColor(Color.MAGENTA);
		rect = new Rect(0 * DEFAULT_WIDTH, 7 * DEFAULT_HEIGHT,
				DEFAULT_WIDTH * 2, DEFAULT_HEIGHT * 9);
		canvas.drawRect(rect, paint);

		// road
		int size = road.length;
		for (int i = 0; i < size; i++) {
			int[] coordinate = road[i];
			canvas.drawBitmap(road_img, coordinate[0] * DEFAULT_WIDTH,
					coordinate[1] * DEFAULT_HEIGHT, paint);
		}
		// soul
		paint = new Paint();
		int coordinate[] = road[currentPos];
		canvas.drawBitmap(soul, coordinate[0] * DEFAULT_WIDTH, coordinate[1]
				* DEFAULT_HEIGHT, paint);
	}
}
